package com.shareach.web.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.query.Order;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.shareach.common.BaseQueryOrder;
import com.shareach.dao.redis.impl.DeployRDao;
import com.shareach.domain.BankCollectDomain;
import com.shareach.domain.BaseBankDomain;
import com.shareach.service.BankCollectService;
import com.shareach.service.BaseBankService;
import com.shareach.util.JsonUtil;
import com.shareach.web.basic.Answer;

@Controller("wbcollect")
@RequestMapping("/wbcollect")
public class BankCollectController extends BaseController {

	@Autowired
	private BankCollectService bankCollectService;
	@Autowired
	DeployRDao deployRDao;
	@Autowired
	private BaseBankService bankService;

	@RequestMapping(value = "/index.htm")
	public ModelAndView index(HttpServletRequest request, String id, Integer hasprocess, Long bankId, String startTime,
			String endTime) {
		ModelAndView mv = new ModelAndView("admin/dic/bcollect/index");
		BaseQueryOrder order = new BaseQueryOrder();
		order.setNumber(getPageNo(request).toString());
		order.setPageSize(getPageSize(request));
		order.addSort("_id", Order.DESCENDING.toString());
		BankCollectDomain domain = new BankCollectDomain();
		// 默认显示未处理
		if (null == hasprocess) {
			domain.setStatus(0);
		} else {
			if (hasprocess != -1) {
				domain.setStatus(hasprocess);
			}
		}
		if (null != bankId) {
			domain.setBank(String.valueOf(bankId));
		}
		if (StringUtils.isNotEmpty(id)) {
			domain.setUrl(id);
		}
		if (StringUtils.isNotEmpty(startTime) || StringUtils.isNotEmpty(endTime)) {
			domain.addDateMap("time", startTime + ":" + endTime+":fuck");
		}
		List<BaseBankDomain> bankList = bankService.findAll();
		Page<BankCollectDomain> collects = bankCollectService.getPageResult(domain, order);
		mv.addObject("result", collects);
		mv.addObject("proecss", hasprocess);
		mv.addObject("banks", bankList);
		mv.addObject("bankId", bankId);
		mv.addObject("startTime", startTime);
		mv.addObject("endTime", endTime);
		return mv;
	}

	@RequestMapping(value = "/changeStatus.htm")
	public void changeStatus(HttpServletRequest request, HttpServletResponse response) {
		String status = request.getParameter("status");
		String id = request.getParameter("id");
		Answer<Object> r = new Answer<Object>();
		if (StringUtils.isEmpty(id) || StringUtils.isEmpty(status)) {
			r.setMsg("为获取数据");
			return;
		}
		try {
			BankCollectDomain dom = bankCollectService.getById(id);
			if (dom.getStatus().intValue() == Integer.parseInt(status)) {
				r.setCode(3);
			} else {

				Integer _status = Integer.valueOf(status);
				BankCollectDomain domain = new BankCollectDomain();
				domain.setUrl(id);
				domain.setStatus(_status);
				domain.setProcessTime(new Date());
				bankCollectService.update(domain);
				if (_status == 1) {
					r.setCode(1);
				} else if (_status == 2) {
					r.setCode(2);
				}
			}
			r.setMsg("完成");
		} catch (NumberFormatException e) {
			r.setMsg("System Error");
		}
		this.outText(response, JsonUtil.jsonFromObject(r));
	}

	@RequestMapping(value = "/edit.htm")
	public ModelAndView bankEdit(HttpServletRequest request, HttpServletResponse response, BankCollectDomain domain,
			String bcollectId) {
		ModelAndView mav = new ModelAndView("admin/dic/bcollect/edit");

		if (domain == null || StringUtils.isEmpty(domain.getUrl())) {
			// redirect
			BankCollectDomain bcollect = bankCollectService.getById(bcollectId);
			mav.addObject("bcollect", bcollect);
			return mav;
		} else {
			Answer<Object> r = new Answer<Object>();
			try {
				BankCollectDomain bcollect = bankCollectService.getById(domain.getUrl());
				BankCollectDomain change = bcollect.compareTo(domain);
				change.setUrl(domain.getUrl());
				change.setProcessTime(new Date());
				// edit
				bankCollectService.update(change);
				// update redis
				// resetRedisDeployed(DeployType.Place, domain.getUrl());
				this.outText(response, JsonUtil.jsonFromObject(r));
			} catch (Exception e) {
				r.setCmd(-1);
				r.setMsg("System Error");
				this.outText(response, JsonUtil.jsonFromObject(r));
				e.printStackTrace();
			}
			return mav;
		}
	}
}
